package controleabastecimento2.activies;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import controleabastecimento2.dao.DAOVeiculo;
import controleabastecimento2.model.Veiculo;


public class CadastrarVeiculo extends Activity {

	static final String GASOLINA = "Gasolina";
	static final String ALCOOL = "Alcool";
	static final String DIESEL = "Diesel";
	
	private Button btnSalvar, btnCancelar, btnVoltar;
	private EditText edtModelo, edtPlaca;
	private RadioButton rdbGasolina, rdbAlcool, rdbDiesel;
	private DAOVeiculo daoVeiculo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cadastro_veiculo);
		
		daoVeiculo = new DAOVeiculo(getBaseContext());
		edtModelo = (EditText)findViewById(R.id.edtModelo);
		edtPlaca = (EditText)findViewById(R.id.edtPlaca);
		rdbGasolina = (RadioButton)findViewById(R.id.rdbGasolina);
		rdbAlcool = (RadioButton)findViewById(R.id.rdbAlcool);
		rdbDiesel = (RadioButton)findViewById(R.id.rdbDiesel);
		btnSalvar = (Button)findViewById(R.id.btnSalvar);
		btnCancelar = (Button)findViewById(R.id.btnCancelar);
		btnVoltar = (Button)findViewById(R.id.btnVoltar);
		
		btnSalvar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try{
					salvar();
					edtModelo.setText("");
					edtPlaca.setText("");
					rdbGasolina.setSelected(false);
					rdbAlcool.setSelected(false);
					rdbDiesel.setSelected(false);
					
					Toast.makeText(CadastrarVeiculo.this, "SUCESSO.", Toast.LENGTH_LONG).show();
				}catch(SQLException e){
					Toast.makeText(CadastrarVeiculo.this, "Falha ao gravar.", Toast.LENGTH_SHORT).show();
				}
			}

				/*if(rdbGasolina.isSelected()){
					ve.setCombustivel(GASOLINA);
				} else if(rdbAlcool.isSelected()){
					ve.setCombustivel(ALCOOL);
				} else {
					ve.setCombustivel(DIESEL);
				}*/
				
		});
		
		btnVoltar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent(CadastrarVeiculo.this, MenuActivity.class);
				startActivity(it);
				CadastrarVeiculo.this.finish();
			}
		});
		
		btnCancelar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				edtModelo.setText("");
				edtPlaca.setText("");
				rdbGasolina.setSelected(false);
				rdbAlcool.setSelected(false);
				rdbDiesel.setSelected(false);
			}
		});
		
	}

	public void onPause(){
		super.onPause();
		setResult(RESULT_CANCELED);
		CadastrarVeiculo.this.finish();
	}
	
	public void salvar(){
		Veiculo veiculo = new Veiculo();
		
		veiculo.setModelo(edtModelo.getText().toString());
		veiculo.setPlaca(edtPlaca.getText().toString());
		if(rdbGasolina.isSelected()){
			veiculo.setCombustivel(GASOLINA);
		} else if(rdbAlcool.isSelected()){
			veiculo.setCombustivel(ALCOOL);
		} else {
			veiculo.setCombustivel(DIESEL);
		}
		
		salvarVeiculo(veiculo);
		setResult(RESULT_OK, new Intent());
	}
	
	protected void salvarVeiculo(Veiculo veiculo){
		daoVeiculo.inserir(veiculo);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.cadastro_veiculo, menu);
		return true;
	}

}